 $(function(){
	
	
	$("#content").width( getIframeSize().width -$("#menuList").outerWidth(true));
	$("#menuList").height(getIframeSize().height);
	init();
	$('#menuList').fileTree({root: '', script: 'function.do?type=functionMenu' }, function(file,name,id) {  
      		openFile(file,name,id);  
    },function(file,name,id){ openMenu(file,name,id);  });
	
	//方法权限加载
	if(!p_lookList){
		$("#lookList").html("");
	}
	
	if(!p_addNewNode){
		$("#addNode").html("");
	}
	
	
});

function openFile(file,name,id)
{
	//loadingSelectedRowData(id);
	// if leaf, don't add child node
	//$("#detailDialog").dialog("open");
	
	$("#functionMenuID").val(id);
	$("#functionMenuName").val(name);
	$("#addChildNodeButton").hide();
	$("#buttonPanel button:first").focus();
}

function openMenu(file,name,id)
{
	//loadingSelectedRowData(id);
	// if leaf, don't add child node
	//$("#detailDialog").dialog("open");
	$("#functionMenuID").val(id);
	$("#functionMenuName").val(name);
	$("#addChildNodeButton").show();
	$("#buttonPanel button:first").focus();
}


function init()
{
	$.ajaxSetup({cache:false});
	//initGrid();
	$("#creatDate").datepicker({
	 	dateFormat:'yy-mm-dd',
	 	
	 	beforeShow: function (i, e) { 
                var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4; 
                e.dpDiv.css('z-index', z); 
        } 
	 	
	 });
	initDialog();
	initDialog1();
	$("#viewButton").click(function(){
		if($.trim($("#functionMenuID").val()).length === 0)
		{
			alert("请先选择查看子节点");
			return;
		}
		
		loadingSelectedRowData($("#functionMenuID").val());
		$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('新增')").hide();
		if(p_save){
			$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").show();
		}else{
			$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").hide();
		}
		
		if(p_delete){
			$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").show();
		}else{
			$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
		}
		$("#detailDialog").dialog("open");
		
	});
	
	$("#addChildNodeButton").click(function(){
		if($.trim($("#functionMenuID").val()).length === 0)
		{
			alert("请先选择增加子节点父功能结点");
			return;
		}
		
		
		processAdd();
	
		
	});
	
	
}

function initDialog()
{
	$("#detailDialog").dialog({
			bgiframe: true,
			autoOpen:false,
			width:620,
			stack:false,
			modal:true,
			position:'center',			
			title:'详细信息',
			buttons:{"取消":function(){$("#detailDialog").dialog("close");},"删除":deleteFunctionMenu,"保存":updateFunctionMenu,"新增": addFunctionMenu,"高级维护": maintain},
			open:function(){
				$("#detailDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
			}
	});
}

function initDialog1()
{
	$("#detailDialog1").dialog({
			bgiframe: true,
			autoOpen:false,
			width:620,
			stack:false,
			modal:true,
			position:'center',			
			title:'高级功能维护',
			buttons:{"取消":function(){$("#detailDialog1").dialog("close");},"删除":deleteMaintain,"修改":updateMaintain,"保存":saveMaintain},
			open:function(){
				$("#detailDialog1").parent().find(".ui-dialog-buttonpane button:visible").focus();
			}
			
	});
}


function initGrid()
{
	jQuery("#functionMenuList").jqGrid({
	   	url:'functionMenu.do?type=list',
	   	height:450,
	   	
		datatype: "xml",
	   	colNames:['menuID','menuName','menuAction','menuLabel','menuView','isLeaf','menuLevel','rootID','parentID','displayOrder','inputValue','inputKey','menuSeq','useType'],		
	
	   	colModel:[
	   	          	{name:'menuID', width:100,xmlmap:'menuID'},
	   	          	{name:'menuName', width:100,xmlmap:'menuName'},
	   	          	{name:'menuAction', width:100,xmlmap:'menuAction'},
	   	          	{name:'menuLabel', width:100,xmlmap:'menuLabel'},
	   	          	{name:'menuView', width:100,xmlmap:'menuView'},
	   	          	{name:'isLeaf', width:100,xmlmap:'isLeaf'},
	   	          	{name:'menuLevel', width:100,xmlmap:'menuLevel'},
	   	          	{name:'rootID', width:100,xmlmap:'rootID'},
	   	          	{name:'parentID', width:100,xmlmap:'parentID'},
	   	          	{name:'displayOrder', width:100,xmlmap:'displayOrder'},
	   	          	{name:'inputValue', width:100,xmlmap:'inputValue'},
	   	          	{name:'inputKey', width:100,xmlmap:'inputKey'},
	   	          	{name:'menuSeq', width:100,xmlmap:'menuSeq'},
	   	          	{name:'useType', width:100,xmlmap:'useType'}
	   	],
	   	xmlReader:{
	   		repeatitems:false
	   	},
	   	pager: $('#functionMenuPager'),
	   	pgbuttons: false,
		pginput: false,
	   	rowNum:10,
	   	autowidth: true,
	   	sortname: 'id',
	    viewrecords: true,
    	caption:"列表"
	}).navGrid('#functionMenuPager',{edit:false,add:false,del:false,refresh:false,search:false}).navButtonAdd("#functionMenuPager",{
			caption : "删除",
			title: '删除所选择记录',
			buttonicon : 'ui-icon-trash',
			onClickButton:processDelete,
			position : "first",
			cursor : 'pointer'
	}).navButtonAdd("#functionMenuPager",{
			caption : "编辑",
			title: '查看所选择记录明细',
			buttonicon : 'ui-icon-pencil',
			onClickButton:processEdit,
			position : "first",
			cursor : 'pointer'
	}).navButtonAdd("#functionMenuPager",{
			caption : "新增",
			title: '增加新记录',
			buttonicon : 'ui-icon-plus',
			onClickButton:processAdd,
			position : "first",
			cursor : 'pointer'
	});
	
}

function initDialogForm(type)
{
	$("#detailDialog input").each(function(){
		$(this).val("");
	});
	
}

function loadingSelectedRowData(id)
{
	 var selectedRowID = id?id:$("#functionMenuList").getGridParam("selrow");
	 $.post(
	 	"functionMenu.do?type=query",
	 	{
	 		menuID:selectedRowID
	 	},
	 	function(xml)
	 	{
			$("#detailDialog #menuID").val( $(xml).find("FunctionMenu menuID").text());
			$("#detailDialog #menuName").val( $(xml).find("FunctionMenu menuName").text());
			$("#detailDialog #menuAction").val( $(xml).find("FunctionMenu menuAction").text());
			$("#detailDialog #menuLabel").val( $(xml).find("FunctionMenu menuLabel").text());
			$("#detailDialog #menuView").val( $(xml).find("FunctionMenu menuView").text());
			$("#detailDialog #isLeaf").val( $(xml).find("FunctionMenu isLeaf").text());
			$("#detailDialog #menuLevel").val( $(xml).find("FunctionMenu menuLevel").text());
			$("#detailDialog #rootID").val( $(xml).find("FunctionMenu rootID").text());
			$("#detailDialog #parentID").val( $(xml).find("FunctionMenu parentID").text());
			$("#detailDialog #displayOrder").val( $(xml).find("FunctionMenu displayOrder").text());
			$("#detailDialog #inputValue").val( $(xml).find("FunctionMenu inputValue").text());
			$("#detailDialog #inputKey").val( $(xml).find("FunctionMenu inputKey").text());
			$("#detailDialog #menuSeq").val( $(xml).find("FunctionMenu menuSeq").text());
			$("#detailDialog #useType").val( $(xml).find("FunctionMenu useType").text());
			
			if($(xml).find("FunctionMenu isLeaf").text() === 'Y')
			{
				$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加子结点')").hide();
			}
			else
			{
				$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加子结点')").show();
			}
	 	},
	 	"xml"
	 );
}

function initAddNewChild()
{
		$("#detailDialog #menuLevel").val( ($("#functionMenuID").val().length/2)+1);
		$("#detailDialog #parentID").val( $("#functionMenuID").val());
}


function processAdd()
{
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('新增')").show();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").hide();
	initDialogForm("insert");
	initAddNewChild();
	$("#detailDialog").dialog("open");
}



function processEdit()
{
	var selectedRowID = $("#functionMenuList").getGridParam("selrow");
	
	 if(!selectedRowID)
	 {
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"functionMenuList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").show();
	initDialogForm("update");
	loadingSelectedRowData();
	$("#detailDialog").dialog("open");
}

function processDelete()
{
	var selectedRowID = $("#functionMenuList").getGridParam("selrow");
	
	 if(!selectedRowID)
	 {
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"functionMenuList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	if(window.confirm("确认删除"))
	{
		$.post(
		"functionMenu.do?type=delete",
		{
			menuID:selectedRowID
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1)
			 {
			 	$("#functionMenuList").delRowData( selectedRowID);
			 	alert("删除成功");
			 	
			 }
			 else
			 {
			 	alert(body);
			 }
						 
		},
		"xml"
		);
	}
}


function validOpertor()
{
	var validFlag = true;
	
	$("#detailDialog input").each(function(){
		
		if(validFlag && $(this).next("span.required").size()>0  && $.trim($(this).val()).length === 0)
		{
			alert($(this).prev("label").html() + "不能为空");
			$(this).focus();
			validFlag = false;
			return;
		}
	});
	return validFlag;
}

function maintain()
{
	loadingDetail1($("#functionMenuID").val());
	$("#detailDialog1").dialog("open");
}

function saveMaintain()
{
	//alert("保存高级维护");
	var postData = 
	{
			functionID:$("#menuID1").val(),
	        creater:$("#creater").val(),
	        creatDate:$("#creatDate").val(),
	        describe:$("#describe").val(),
	        demand:$("#demand").val(),
			detial:$("#detial").val(),
			test:$("#test").val()
	};
	$.ajax({
		type: 'POST',
		url:  "functionMenu.do?type=insertEFunction",
		dataType: "json",
		data: postData,
		success: function(data){
		    if(data.flag==1){
		    	alert("保存成功！");
		    }else{
		    	alert("保存失败！");
		    }
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){					    		
			alert("系统错误，请联系管理员");
		}
	});
}

function updateMaintain()
{
	//alert("修改高级维护");
	var postData = 
	{
			functionID:$("#menuID1").val(),
	        creater:$("#creater").val(),
	        creatDate:$("#creatDate").val(),
	        describe:$("#describe").val(),
	        demand:$("#demand").val(),
			detial:$("#detial").val(),
			test:$("#test").val()
	};
	$.ajax({
		type: 'POST',
		url:  "functionMenu.do?type=updateEFunction",
		dataType: "json",
		data: postData,
		success: function(data){
		    if(data.flag!=0){
		    	alert("修改成功！");
		    }else{
		    	alert("修改失败！");
		    }
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){					    		
			alert("系统错误，请联系管理员");
		}
	});
}

function deleteMaintain(){
	
	if(window.confirm("确认删除要删除该高级维护")){
		var postData = 
		{
				functionID:$("#menuID1").val()
		};
		$.ajax({
			type: 'POST',
			url:  "functionMenu.do?type=deleteEFunction",
			dataType: "json",
			data: postData,
			success: function(data){
			    if(data.flag!=0){
			    	alert("删除成功！");
			    	$("#detailDialog1").dialog("close");
			    }else{
			    	alert("删除失败！");
			    }
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){					    		
				alert("系统错误，请联系管理员");
			}
		});
	}
}

function downLoad(i){
	var fileName = null;
	if(i==1){
		fileName = "demand";
	}else if(i==2){
		fileName = "detail";
	}else{
		fileName = "test";
	}
	var postData = 
	{
			functionID:$("#menuID1").val(),
	        downFile:fileName
	};
	$.ajax({
		type: 'POST',
		url:  "functionMenu.do?type=donwnLoacDOC",
		dataType: "json",
		data: postData,
		success: function(data){
		    if(data.flag==1){
		    	alert("文件保存在d盘根目录下！");
		    }else{
		    	alert("下载失败，请联系管理员！");
		    }
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){					    		
			alert("系统错误，请联系管理员");
		}
	});
}

function loadingDetail1(id)
{
	    var selectedRowID = id;
	    var postData = {functionID:selectedRowID};
		$.ajax({
			type: 'POST',
			url:  "functionMenu.do?type=selectEFunction",
			dataType: "json",
			data: postData,
			success: function(data){
			var result = data;	
			$("#menuID1").val(result.functionID);
			$("#creater").val(result.creater);
			$("#creatDate").val(result.creatDate);
			$("#newDate").val(result.newDate);
			$("#describe").val(result.describe);
			$("#demand").val(result.demandDOC);
			$("#detial").val(result.detialDOC);
			$("#test").val(result.testDOC);
			$("#demandName").text(result.demandName);
			$("#demandName").attr("href","./uploadPic/"+result.demandName);
			$("#detialName").text(result.detialName);
			$("#detialName").attr("href","./uploadPic/"+result.detialName);
			$("#testName").text(result.testName);
			$("#testName").attr("href","./uploadPic/"+result.testName);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){					    		
				alert("系统错误，请联系管理员");
			}
		});
}

function addFunctionMenu()
{
	if(validOpertor() === false)
	{
		return;
	}
	$.post(
		"functionMenu.do?type=insert",
		{
			
			menuName:$("#detailDialog #menuName").val(),
			menuAction:$("#detailDialog #menuAction").val(),
			menuLabel:$("#detailDialog #menuLabel").val(),
			menuView:$("#detailDialog #menuView").val(),
			isLeaf:$("#detailDialog #isLeaf").val(),
			menuLevel:$("#detailDialog #menuLevel").val(),
			rootID:$("#detailDialog #rootID").val(),
			parentID:$("#detailDialog #parentID").val(),
			displayOrder:$("#detailDialog #displayOrder").val(),
			inputValue:$("#detailDialog #inputValue").val(),
			inputKey:$("#detailDialog #inputKey").val(),
			menuSeq:$("#detailDialog #menuSeq").val(),
			useType:$("#detailDialog #useType").val()
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1)
			 {
			 	//var dataRow = 
			 	//{
					//menuID:$("#detailDialog #menuID").val(),
					//menuName:$("#detailDialog #menuName").val(),
					//menuAction:$("#detailDialog #menuAction").val(),
					//menuLabel:$("#detailDialog #menuLabel").val(),
					//menuView:$("#detailDialog #menuView").val(),
					//isLeaf:$("#detailDialog #isLeaf").val(),
					//menuLevel:$("#detailDialog #menuLevel").val(),
					//rootID:$("#detailDialog #rootID").val(),
					//parentID:$("#detailDialog #parentID").val(),
					//displayOrder:$("#detailDialog #displayOrder").val(),
					//inputValue:$("#detailDialog #inputValue").val(),
					//inputKey:$("#detailDialog #inputKey").val(),
					//menuSeq:$("#detailDialog #menuSeq").val(),
					//useType:$("#detailDialog #useType").val()
			 	//};
			 	
			 	//$("#functionMenuList").addRowData( $('#detailDialog #menuID').val(),dataRow);
			 	
			 	
			 	
			 	$("#menuList #"+$("#detailDialog #parentID").val()).click();
			 	$("#menuList #"+$("#detailDialog #parentID").val()).click();
			 	
			 	alert("新增成功");
			 	$("#detailDialog").dialog("close");
			 }
			 else
			 {
			 	alert(body);
			 }
		},
		"xml"
	);
}
function updateFunctionMenu()
{
	if(validOpertor() === false)
	{
		return;
	}
	$.post(
		"functionMenu.do?type=update",
		{
			menuID:$("#detailDialog #menuID").val(),
			menuName:$("#detailDialog #menuName").val(),
			menuAction:$("#detailDialog #menuAction").val(),
			menuLabel:$("#detailDialog #menuLabel").val(),
			menuView:$("#detailDialog #menuView").val(),
			isLeaf:$("#detailDialog #isLeaf").val(),
			menuLevel:$("#detailDialog #menuLevel").val(),
			rootID:$("#detailDialog #rootID").val(),
			parentID:$("#detailDialog #parentID").val(),
			displayOrder:$("#detailDialog #displayOrder").val(),
			inputValue:$("#detailDialog #inputValue").val(),
			inputKey:$("#detailDialog #inputKey").val(),
			menuSeq:$("#detailDialog #menuSeq").val(),
			useType:$("#detailDialog #useType").val()
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1)
			 {
			 	//var dataRow = 
			 	//{
					//	menuID:$("#detailDialog #menuID").val(),
					//	menuName:$("#detailDialog #menuName").val(),
					//	menuAction:$("#detailDialog #menuAction").val(),
					//	menuLabel:$("#detailDialog #menuLabel").val(),
					//	menuView:$("#detailDialog #menuView").val(),
					//	isLeaf:$("#detailDialog #isLeaf").val(),
					//	menuLevel:$("#detailDialog #menuLevel").val(),
					//	rootID:$("#detailDialog #rootID").val(),
					//	parentID:$("#detailDialog #parentID").val(),
					//	displayOrder:$("#detailDialog #displayOrder").val(),
					//	inputValue:$("#detailDialog #inputValue").val(),
					//	inputKey:$("#detailDialog #inputKey").val(),
					//	menuSeq:$("#detailDialog #menuSeq").val(),
					//	useType:$("#detailDialog #useType").val()
			 	//};
			 	
			 	//$("#functionMenuList").setRowData( $("#detailDialog #menuID").val(),dataRow);
			 	
			 	if($("#detailDialog #isLeaf").val() === 'Y')
			 	{
			 		$("#menuList #"+$("#detailDialog #menuID").val()).attr("rel",$("#detailDialog #menuAction").val());
			 	}
			 	else
			 	{
			 		$("#menuList #"+$("#detailDialog #menuID").val()).attr("rel",$("#detailDialog #menuID").val());
			 	}
			 	
			 	$("#menuList #"+$("#detailDialog #menuID").val()).attr("title",$("#detailDialog #menuName").val()).html($("#detailDialog #menuName").val());
			 	
			 	$("#functionMenuName").val($("#detailDialog #menuName").val());
			 	
			 	alert("更新成功");
			 	$("#detailDialog").dialog("close");
			 }
			 else
			 {
			 	alert(body);
			 }
		},
		"xml"
	);
}

function deleteFunctionMenu()
{
	if(window.confirm("确认删除"))
	{
		$.post(
			"functionMenu.do?type=delete",
			{
				menuID:$("#detailDialog #menuID").val()
			},
			function(xml)
			{
				 var flag   = parseInt($(xml).find("flag").text());
			 	var body = $(xml).find("body").text();
				 if(flag === 1)
			 	{
			 		//1,delete tree node
			 		//2,clear contentPanel
			 		//3,close dialog
			 		
			 		$("#menuList #"+$("#detailDialog #menuID").val()).parent().remove();
			 		$("#contentPanel input:text").val("");
			 		alert("更新成功");
			 		$("#detailDialog").dialog("close");
			 	}
			 	else
			 	{
			 		alert(body);
			 	}
			},
			"xml"
		);
	}
}
